package middlewares

import (
	"github.com/gin-gonic/gin"
	"local/databases/redis"
)

// 定义登录验证中间件
func LoginMiddleWare(c *gin.Context) {
	//获取username与token
	username := c.Request.Header.Get("username")
	token := c.Request.Header.Get("token")

	if len(username) == 0 || len(token) == 0 {

		c.JSON(441, gin.H{
			"code": 441,
			"msg":  "请先登录",
		})
		c.Abort()
		return
	}

	ok, checkToken := redis.GetToken(username)

	if ok {
		if token == checkToken {
			//交给下一个路由处理
			c.Set("ok", 1)
		}
		return
	}
	if !ok {

		c.JSON(441, gin.H{
			"code": 441,
			"msg":  "请先登录",
		})
		c.Abort()
		return
	}
}
